<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="130px">
      <div v-if="this.biztype.value === 1">
        <el-form-item
          label="配置名称"
          prop="name"
          :rules="[{ required:true,message:'请输入配置名称',trigger:['blur','change'] }]"
        >
          <el-input v-model="editPram.name" maxlength="20" placeholder="配置名称" />
        </el-form-item>
        <el-form-item label="父级">
          <el-cascader v-model="editPram.pid" :disabled="isCreate ===1 && editPram.pid ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
          <!-- allTreeList or parentOptions-->
        </el-form-item>
        <el-form-item label="负责单位" prop="responsibleUnit" :rules="[{ required:true,message:'请至少选择一项',trigger:['blur','change'] }]">
          <el-select v-model="editPram.responsibleUnit" placeholder="负责单位" clearable multiple style="width: 100%">
            <el-option
              v-for="(item,index) in departmentList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="content" :rules="[{ required:true,message:'请输入描述',trigger:['blur','change'] }]">
          <el-input v-model="editPram.content" type="textarea" :rows="7" placeholder="描述" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="editPram.sort" :min="0"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="editPram.status"  active-text="显示"
                     inactive-text="隐藏" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')" v-hasPermi="['admin:system:category:save']">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<!--创建和编辑公用一个组件-->
<script>
import * as categoryApi from '@/api/categoryApi.js'
import * as roleApi from '@/api/role.js'
import * as selfUtil from '@/utils/ZBKJIutil.js'
import {addCategroyForDepartment} from "@/api/categoryApi.js";
export default {
  // name: "edit"
  props: {
    prent: {
      type: Object,
      required: true
    },
    isCreate: {
      type: Number,
      default: 0
    },
    biztype: {
      type: Object,
      required: true
    },
    editData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        name: null,
        shortName: null,
        pid: null,
        sort: 0,
        status: 1,
        isCards: 0,
        level: null,
        responsibleUnit:[],
        content:null,
        id: 0
      },
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      parentOptions: [],
      levelList: [],
      departmentList:[],
    }
  },
  mounted() {
    this.initEditData()
    this.handleGetDepartmentList()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    handleGetDepartmentList() {
      /*const _pram = {
        page: 1,
        limit: this.constants.page.limit[4],
      }*/
      roleApi.getCommonLevel().then(data => {
     // let data = this.constants.staticDepartmentList
        this.departmentList = data;

      let arr = [];
      data.forEach(item=>{
        arr.push(item.id);
      })
      if(!arr.includes(Number.parseInt(this.editPram.responsibleUnit))){
        this.$set(this.editPram,'responsibleUnit',[]);
      }
      })
    },
    initEditData() {
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
      const { name, shortName, pid, sort, status, id, isCards, level, responsibleUnit, content } = this.editData
      if(this.isCreate === 1){
        this.editPram.name = name
        this.editPram.shortName = shortName
        this.editPram.pid = pid
        this.editPram.sort = sort
        this.editPram.status = status
        this.editPram.isCards = isCards
        this.editPram.level = level
        const _departmentIds = []
        if (responsibleUnit.length > 0 && !responsibleUnit.includes(',')) {
          //如果权限id集合有长度并且是只有一个，就将它Push进_departmentIds这个数组
          _departmentIds.push(Number.parseInt(responsibleUnit))
        } else {
          //否则就将多个id集合解构以后push进departmentIds并且转换为整型
          _departmentIds.push(...responsibleUnit.split(',').map(item => Number.parseInt(item)))
        }
        this.editPram.responsibleUnit = _departmentIds
        this.editPram.content = content
        this.editPram.id = id
      }else{
        this.editPram.pid = this.prent.id
      }
    },
    //处理哪一级别禁用
    addTreeListLabelForCasCard(arr, child) {
      arr.forEach((o,i) => {
        if (o.child && o.child.length) {
          // o.disabled = true
          o.child.forEach((j) => {
            j.disabled = true
          })
        }
      })
    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      const form = this.editPram;
      form.responsibleUnit = this.editPram.responsibleUnit.join(',')
      if (isSave) {
        form.pid = this.prent.id
        this.loadingBtn = true
        categoryApi.addCategroy(form,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('创建成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      } else {
        form.pid = Array.isArray(form.pid) ? form.pid[0] : form.pid
        this.loadingBtn = true
        categoryApi.addCategroy(form,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('更新成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
